﻿using UnityEngine;

public class MinifigController : PlayerController
{
    private const float UpdateInterval = 0.2f;
    private Animator _animator;
    private float _secondsWaited;

    public override void Start()
    {
        base.Start();
        _animator = GetComponent<Animator>();
        Respawn();
    }

    public override void Update()
    {
        base.Update();
        _animator.SetBool("IsJumping", IsJumping);
        _animator.SetBool("IsWalking", IsWalking || IsTurning);
        if (_animator.GetCurrentAnimatorStateInfo(0).shortNameHash == Animator.StringToHash("Jump"))
        {
            MovementSpeed = -20;
        }
        else
        {
            MovementSpeed = -15;
        }
        _secondsWaited += Time.deltaTime;
        if (_secondsWaited >= UpdateInterval)
        {
            _secondsWaited = 0.0f;
        }
    }

    public void Respawn()
    {
        transform.position = GameObject.FindGameObjectWithTag("Respawn").transform.position;
        transform.rotation = GameObject.FindGameObjectWithTag("Respawn").transform.rotation;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag.ToLower() == "water")
        {
            Respawn();
        }
    }
}